package com.crm.business.crm.controller;


import com.crm.business.crm.service.CrmUploadExcelService;
import com.crm.business.crm.service.ICrmHeadquartersBusinessService;
import com.crm.common.crm.constant.CrmEnum;
import com.crm.common.crm.entity.BO.CrmModelSaveBO;
import com.crm.common.crm.entity.BO.CrmRoiSearchBO;
import com.crm.common.crm.entity.BO.CrmUpdateInformationBO;
import com.crm.common.crm.entity.BO.UploadExcelBO;
import com.crm.common.crm.entity.PO.CrmHeadquartersBusiness;
import com.crm.common.ka.core.common.R;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.entity.BasePage;
import com.crm.business.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/crmHeadquartersBusiness")
@Api(tags = "zo充值模块接口")
public class CrmHeadquartersBusinessController {

    @Autowired
    private ICrmHeadquartersBusinessService crmHeadquartersBusinessService;

    @Autowired
    private CrmUploadExcelService uploadExcelService;

    @PostMapping("/queryById/{putinMoneyId}")
    @ApiOperation("根据ID查询")
    public Result<CrmHeadquartersBusiness> queryById(@PathVariable("putinMoneyId") @ApiParam(name = "id", value = "id") Integer putinMoneyId) {
        CrmHeadquartersBusiness crmHeadquartersBusiness = crmHeadquartersBusinessService.getById(putinMoneyId);
        return R.ok(crmHeadquartersBusiness);
    }

    @PostMapping("/deleteByIds")
    @ApiOperation("根据ID删除数据")
//    @SysLogHandler(behavior = BehaviorEnum.DELETE)
    public Result deleteByIds(@RequestBody CrmHeadquartersBusiness crmHeadquartersBusiness) {
        crmHeadquartersBusinessService.deleteByIds(crmHeadquartersBusiness);
        return R.ok();
    }

    @PostMapping("/queryPageList")
    @ApiOperation("查询列表页数据")
    @ResponseBody
    public Result<BasePage<CrmHeadquartersBusiness>> queryPageList(@RequestBody CrmRoiSearchBO search) {
        BasePage<CrmHeadquartersBusiness> mapBasePage = crmHeadquartersBusinessService.queryPageList(search);
        return R.ok(mapBasePage);
    }

    @PostMapping("/queryListById/{putinMoneyId}")
    @ApiOperation("根据ID查询多个")
    public Result<CrmHeadquartersBusiness> queryListById(@PathVariable("putinMoneyId") @ApiParam(name = "id", value = "id") Integer id){
        CrmHeadquartersBusiness crmPutinMoney = crmHeadquartersBusinessService.getListById(id);
        return R.ok(crmPutinMoney);
    }

    @PostMapping("/add")
    @ApiOperation("保存数据")
    public Result add(@RequestBody CrmModelSaveBO crmModel) {
        crmHeadquartersBusinessService.addOrUpdate(crmModel);
        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改数据")
    public Result update(@RequestBody CrmModelSaveBO crmModel) {
        crmHeadquartersBusinessService.addOrUpdate(crmModel);
        return R.ok();
    }

    @PostMapping("/uploadExcel")
    @ApiOperation("导入投放金额")
//    @SysLogHandler(behavior = BehaviorEnum.EXCEL_IMPORT,object = "导入投放金额",detail = "导入投放金额")
    public Result<Long> uploadExcel(@RequestParam("file") MultipartFile file, @RequestParam("ownerUserId") Long ownerUserId, @RequestParam("repeatHandling") Integer repeatHandling) {
        UploadExcelBO uploadExcelBO = new UploadExcelBO();
        uploadExcelBO.setOwnerUserId(ownerUserId);
        uploadExcelBO.setUserInfo(UserUtil.getUser());
//        uploadExcelBO.setCrmEnum(CrmEnum.ROI_INVESTMENT);
        uploadExcelBO.setPoolId(null);
        uploadExcelBO.setRepeatHandling(repeatHandling);
        Long messageId = uploadExcelService.uploadExcel(file, uploadExcelBO);
        return R.ok(messageId);
    }

    @PostMapping("/updateInformation")
    @ApiOperation("基本信息保存修改")
    public Result updateInformation(@RequestBody CrmUpdateInformationBO updateInformationBO) {
        crmHeadquartersBusinessService.updateInformation(updateInformationBO);
        return R.ok();
    }

    @PostMapping("/downloadExcel")
    @ApiOperation("下载导入模板")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        crmHeadquartersBusinessService.downloadExcel(response);
    }

    @PostMapping("/allExportExcel")
    @ApiOperation("全部导出")
//    @SysLogHandler(behavior = BehaviorEnum.EXCEL_EXPORT,object = "ROI投放金额导出",detail = "全部导出")
    public void allExportExcel(@RequestBody CrmRoiSearchBO search, HttpServletResponse response) {
        search.setPageType(0);
        crmHeadquartersBusinessService.exportExcel(response, search);
    }

    @PostMapping("/batchExportExcel")
    @ApiOperation("选中导出")
//    @SysLogHandler(behavior = BehaviorEnum.EXCEL_EXPORT,object = "ROI投放金额导出",detail = "选中导出")
    public void batchExportExcel(@RequestBody @ApiParam(name = "ids", value = "id列表") List<Integer> ids, HttpServletResponse response) {
        CrmRoiSearchBO search = new CrmRoiSearchBO();
        search.setPageType(0);
        search.setIds(ids);
        crmHeadquartersBusinessService.exportExcel(response, search);
    }

}
